//
//  zhuceViewController.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/15.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "zhuceViewController.h"
#import "NSObject+alertView.h"
#import "LoadViewController.h"
#import <AVOSCloud/AVOSCloud.h>//第三方登录框架
@interface zhuceViewController ()

@property (strong, nonatomic) IBOutlet UITextField *nameTextFired;

@property (strong, nonatomic) IBOutlet UITextField *PassWordTextFired;
@property (strong, nonatomic) IBOutlet UITextField *emailTextFired;

@end

@implementation zhuceViewController

- (void)viewDidLoad {
    [super viewDidLoad];

      self.title = @"注册";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登录" style:(UIBarButtonItemStylePlain) target:self action:@selector(BackAction)];



}

//返回方法

-(void)BackAction
{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//注册
- (IBAction)zhuce:(UIButton *)sender {
   //    signUpOrLoginWithMobilePhoneNumber
    
 //判断邮箱格式
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailpredict = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailRegex];
    
    
    ///判断密码
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    
    // 正则判断一下电话
   // NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
   // NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    
    if ([self.nameTextFired.text isEqualToString:@""]||[self.PassWordTextFired.text isEqualToString:@""]) {
        
        
        [NSObject alterString:@"用户名或密码不能为空"];
    }else if (![emailpredict evaluateWithObject:self.emailTextFired.text])
    {
        [NSObject alterString:@"邮箱格式错误"];
    }else if (![passWordPredicate evaluateWithObject:self.PassWordTextFired.text]){
        [NSObject alterString:@"密码格式错误"];
    }
        
    
    
    
        AVUser *user = [AVUser user];// 新建 AVUser 对象实例
        user.username = self.nameTextFired.text;// 设置用户名
        user.password =  self.PassWordTextFired.text;// 设置密码
        

       user.email = self.emailTextFired.text;// 设置邮箱
    
    
    
    
        
        [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            if (succeeded) {
                
                [NSObject alterString:@"恭喜,注册成功!"];
                
                LoadViewController *load =[LoadViewController new];
                [self.navigationController pushViewController:load animated:YES];
                
            } else {
                NSLog(@"%@",error);
                [NSObject alterString:@"别灰心,在尝试注册一遍!"];
            }
        }];

    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
